import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    current: 0,
    answers: {
      answer1: [],
      answer2: [],
      answer3: []
    },
    isOver: false,
    mobile: '',
    hadListen: true
  },
  mutations: {
    changeCurrent(state, current) {
      console.log(current)
      state.current = current;
      state.answers['answer' + (current + 1)] = []
    },
    changeOver(state) {
      state.isOver = true
    },
    changeMobile(state, str) {
      console.log({ str })
      state.mobile = str
    },
    changeListen(state) {
      state.hadListen = false
    },
    changeAnswers(state, obj) {
      state.answers['answer' + obj.id] = obj.answer
    },
    syncLocalStorage(state) {
      console.log(state.isOver)
      localStorage.setItem("survey-storage", JSON.stringify(state));
    },
    // 同步 localStorage=> state
    syncState(state, { storage }) {
      console.log('savestate')
      Object.keys(storage).forEach(key => {
        if (storage.hasOwnProperty(key)) {
          state[key] = storage[key];
        }
      });
    }
  },
  actions: {

  }
});
